//
//  HWTakeALookDetailProtocol.swift
//  PlayWithProject
//
//  Created by 王昌健 on 2021/8/4.
//

import UIKit

class HWTakeALookDetailProtocol: NSObject,UICollectionViewDelegate,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
    let openCellNum = 2
    var model:HWGameCircleModel?
    var commentModels:[HWTakeALookCommentModel]?
    
    
    var didSelectImage:((_:Int,_:[String])->())?
    var didSelectVideo:((_:String)->())?
    var zanButtonClick:((_:String,_:String)->())?
    
    var jumpToDetail:((_:HWGameCircleModel)->())?
    var jumpToUserDetail:((_:String)->())?
    
    /// 跳转评论列表
    var jumpToCommentDetail:((_:HWTakeALookCommentModel)->())?
    /// 回复某人
    var replyContentName:((_:HWTakeALookCommentModel)->())?
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.section == 0 {
            if model?.video.count ?? 0 > 0 {
                didSelectVideo?(model?.video ?? "")
            }else{
                didSelectImage?(indexPath.row,model?.img ?? [])
            }
            
        }else{
//            if self.commentModels![indexPath.section-1].data_num > openCellNum {
                self.jumpToCommentDetail!(self.commentModels![indexPath.section-1])
//            }
        }
        
    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        
        if kind == UICollectionView.elementKindSectionHeader {
            if indexPath.section == 0 {
                let  header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HWTakeALookPageHeader", for: indexPath) as! HWTakeALookPageHeader
                header.model = model
                
                header.jumpToDetail = {
                    self.jumpToDetail!($0)
                }
                
                header.jumpToUserDetail = {
                    self.jumpToUserDetail!($0)
                }
                
                return header
            }else{
                let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HWTakeALookDetailCommentHeader", for: indexPath) as! HWTakeALookDetailCommentHeader
                
                
                //                header.count = commentModels?[indexPath.section-1].data?.count ?? 0
                header.model = commentModels?[indexPath.section-1]
                
                header.jumpToUserDetail = {
                    self.jumpToUserDetail!($0)
                }
                header.zanButtonClick = {
                    self.zanButtonClick!("1",$0)
                }
                header.cellOpenClick = {
                    
                    //                    collectionView.reloadData()
                }
                
                header.replyContentName = {
                    self.replyContentName!($0)
                }
                
                return header
            }
            
            
        }else{
            if indexPath.section == 0 {
                let  footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HWTakeALookPageFooter", for: indexPath) as! HWTakeALookPageFooter
                
                footer.comLabel.isHidden = false
                footer.commentNumLabel.isHidden = false
                if commentModels?.count ?? 0 > 0{
                    footer.comLabel.text = "评价"
                }else{
                    footer.comLabel.text = ""
                }
                if commentModels?.count ?? 0 > 0{
                    footer.commentNumLabel.text = String(format: "%d", commentModels!.count)
                }
                
                footer.model = model
                footer.zanButtonClick = {
                    self.zanButtonClick!("0",$0)
                }
                
                
                footer.jumpToDetail = {
                    self.jumpToDetail!($0)
                }
                
                return footer
            }else{
                let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HWTakeALookDetailCommentFooter", for: indexPath) as! HWTakeALookDetailCommentFooter
                footer.count = commentModels?[indexPath.section-1].data_num ?? 0
                footer.model = commentModels?[indexPath.section-1]
                footer.cellOpenClick = {
                    //                    collectionView.reloadData()
                    self.jumpToCommentDetail!($0)
                    //                    UIApplication.shared.windows.filter({$0.isKeyWindow}).last?.rootViewController?.view.showHUD(title: "别点我")
                }
                return footer
            }
            
            
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 5
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize
    {
        if section == 0 {
            return CGSize(width: screenWidth, height: getTextHeigh(textStr: model?.content ?? "", font: .systemFont(ofSize: 14), width: screenWidth-30) + 95)
        }else{
            //            if commentModels?[section-1].data?.count ?? 0 > openCellNum {
            //                if commentModels?[section-1].isOpen == true {
            //                    return CGSize(width: screenWidth, height: getTextHeigh(textStr: commentModels?[section-1].content ?? "", font: .systemFont(ofSize: 13), width: screenWidth-115)+60)
            //                }else{
            //                    return CGSize(width: screenWidth, height: getTextHeigh(textStr: commentModels?[section-1].content ?? "", font: .systemFont(ofSize: 13), width: screenWidth-115)+90)
            //                }
            //
            //            }else{
            return CGSize(width: screenWidth, height: getTextHeigh(textStr: commentModels?[section-1].content ?? "", font: .systemFont(ofSize: 13), width: screenWidth-115)+60)
            //            }
            
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        if section == 0 {
            if model?.huati.count ?? 0 > 0 {
                return CGSize(width: screenWidth, height: 95+44)
            }else{
                return CGSize(width: screenWidth, height: 70+44)
            }
        }else{
            if commentModels?[section-1].data_num ?? 0 > openCellNum {
                //                if commentModels?[section-1].isOpen == true{
                return CGSize(width: screenWidth, height: 30)
                //                }else{
                //                    return CGSize.zero
                //                }
            }else{
                return CGSize.zero
            }
            
        }
        
        
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        if model != nil {
            return 1 + (commentModels?.count ?? 0)
        }else{
            return 0
        }
        
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 0 {
            if model != nil {
                return model?.img.count ?? 0
            }else{
                return 0
            }
        }else{
            if commentModels?[section-1].data_num ?? 0 > openCellNum {
                if commentModels?[section-1].isOpen == true {
                    return commentModels?[section-1].data?.count ?? 0
                }else{
                    return openCellNum
                }
                
            }else{
                return commentModels?[section-1].data?.count ?? 0
            }
            
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.section == 0 {
            if model!.img.count == 1 {
                return CGSize (width: screenWidth - 30, height: (screenWidth - 30)/2)
            }else if model!.img.count == 2 {
                return CGSize (width: (screenWidth - 30-10)/2, height: (screenWidth - 30-10)/2)
            }else{
                
                return CGSize (width: (screenWidth-40)/3, height: (screenWidth-40)/3)
            }
        }else{
            return CGSize (width: screenWidth, height: getTextHeigh(textStr: commentModels?[indexPath.section-1].data?[indexPath.row].content ?? "", font: UIFont.boldSystemFont(ofSize: 13), width: screenWidth-170)+60)
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let pageCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWGameCircleCell", for: indexPath) as! HWGameCircleCell
        let commentCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWTakeALookDetailCommentCell", for: indexPath) as! HWTakeALookDetailCommentCell
        if indexPath.section == 0 {
            //            if model?.img.count ?? 0 > 0 {
            //                if model?.img.count == 1 {
            //                    pageCell.imageView.snp.updateConstraints { make in
            //                        make.width.equalTo(screenWidth - 30)
            //                        make.height.equalTo((screenWidth - 30)/2)
            //                    }
            //                }else if model?.img.count == 2 {
            //                    pageCell.imageView.snp.updateConstraints { make in
            //                        make.width.height.equalTo((screenWidth - 30-10)/2)
            //                    }
            //                }else{
            //                    pageCell.imageView.snp.updateConstraints { make in
            //                        make.width.height.equalTo((screenWidth-40)/3)
            //                    }
            //                }
            //            }
            pageCell.model = model
            pageCell.imageView.sd_setImage(with: URL(string: model?.img[indexPath.row] ?? ""), completed: nil)
            return pageCell
        }else{
            commentCell.name = commentModels?[indexPath.section-1].pet_name ?? ""
            commentCell.model = commentModels?[indexPath.section-1].data?[indexPath.row]
            commentCell.zanButtonClick = {
                self.zanButtonClick!("1",$0)
            }
            return commentCell
        }
        
    }
    
    
    
    
}
